home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1996 May: Tool Chest / Developer CD Series May 1996 (Tool Chest) (Apple Computer) (1996).iso / Sample Code / Snippets / Hardware / TimerTstƒ / TimerTst.a next >
Encoding:
Text File  |  1992-07-15  |  6.5 KB  |  234 lines  |  [TEXT/MPS ]

  1. ***********************************************************************
  2. ***
  3. ***        TimerTst
  4. ***        
  5. ***
  6. ***********************************************************************
  7.  
  8.  
  9.                     STRING    ASIS
  10.                     INCLUDE 'Traps.a'
  11.                     INCLUDE 'QuickEqu.a'
  12.                     INCLUDE 'SysEqu.a'
  13.                     PRINT    ON
  14.                     BLANKS            OFF     ; allow comments without semicolons
  15.  
  16. T1Arbitrate EQU    $B3F         ; (byte) $FF if Timer T1 up for grabs.    
  17. ; VIA Offsets
  18.  
  19. vBufB            EQU      0            ; BUFFER B
  20. vBufAH            EQU      $200        ; buffer a (with handshake) [ Dont use! ]
  21. vDIRB            EQU      $400        ; DIRECTION B
  22. vDIRA            EQU      $600        ; DIRECTION A
  23. vT1C            EQU      $800        ; TIMER 1 COUNTER (L.O.)
  24. vT1CH            EQU      $A00        ; timer 1 counter (high order)
  25. vT1L            EQU      $C00        ; TIMER 1 LATCH (L.O.)
  26. vT1LH            EQU      $E00        ; timer 1 latch (high order)
  27. vT2C            EQU      $1000        ; TIMER 2 LATCH (L.O.)
  28. vT2CH            EQU      $1200        ; timer 2 counter (high order)
  29. vSR             EQU      $1400        ; SHIFT REGISTER
  30. vACR            EQU      $1600        ; AUX. CONTROL REG.
  31. vPCR            EQU      $1800        ; PERIPH. CONTROL REG.
  32. vIFR            EQU      $1A00        ; INT. FLAG REG.
  33. vIER            EQU      $1C00        ; INT. ENABLE REG.
  34. vBufA            EQU      $1E00        ; BUFFER A
  35.  
  36. vBufD            EQU      $1E00        ; disk head select buffer
  37.  
  38. *********
  39. * Types *
  40. *********
  41.  
  42. * To illustrate how the template type feature works the following templates are
  43. * declared and used instead of the simple offsets defined in the equate files.
  44. * By using these, the Assember source appromixates very closely the Pascal
  45. * source for referencing the corresponding information.  Perhaps someday we will
  46. * have a set of "equate" files that define types just like Pascal USES units do!
  47.  
  48. Point            RECORD            0            Point = RECORD CASE INTEGER OF
  49. v                DS.W            1                      1: (v: INTEGER;
  50. h                DS.W            1                          h: INTEGER);
  51.                 ORG             v                      2: (vh: ARRAY[1..2]
  52. vh                DS.W            h                                 OF INTEGER)
  53.                 ENDR                                END;
  54.  
  55. Rect            RECORD            0            Rect  = RECORD CASE INTEGER OF
  56. top             DS.W            1                      1: (top:      INTEGER;
  57. left            DS.W            1                          left:   INTEGER;
  58. bottom            DS.W            1                          bottom: INTEGER;
  59. right            DS.W            1                          right:  INTEGER);
  60.                 ORG             top
  61. topLeft         DS.L            Point                  2:  (topLeft:  Point;
  62. botRight        DS.L            Point                  3:  (botRight: Point)
  63.                 ENDR                                END;
  64.  
  65. BitMap            RECORD            0            BitMap = RECORD
  66. baseAddr        DS.L            1                      baseAddr: QDPtr;
  67. rowBytes        DS.W            1                      rowBytes: INTEGER;
  68. bounds            DS.L            Rect                  bounds:     Rect
  69.                 ENDR                                END;
  70.  
  71. EventRecord     RECORD            0            EventRecord = RECORD
  72. what            DS.W            1                      what:    INTEGER;
  73. message         DS.L            1                      message:     LONGINT;
  74. when            DS.L            1                      when:    LONGINT;
  75. where            DS.L            Point                  where:       Point;
  76. modifiers        DS.W            1                      modifiers: INTEGER
  77.                 ENDR                                END;
  78.  
  79.                 EJECT
  80.  
  81. ***********************
  82. * QuickDraw's Globals *
  83. ***********************
  84.  
  85. * The following data module is used to define the QuickDraw global data area.
  86. *                -----------
  87.  
  88. QuickDraw        RECORD            ,DECREMENT
  89. thePort         DS.L            1
  90. white            DS.B            8
  91. black            DS.B            8
  92. gray            DS.B            8
  93. ltGray            DS.B            8
  94. dkGray            DS.B            8
  95. arrow            DS.B            cursRec
  96. screenBits        DS.B            BitMap
  97. randSeed        DS.L            1
  98.                 ORG             -grafSize
  99.                 ENDR
  100.  
  101. ******************************** Entry *******************************
  102.  
  103. Entry    MAIN
  104.         ALIGN 2
  105.         WITH            QuickDraw
  106.         bra.s        real                            ;jump around handler and globals
  107.         
  108. ******************************* Globals ******************************
  109.  
  110. oldVector    DC.L    0                                ;old timer 1 vector
  111. valid        DC.B    0                                ;validity check for app
  112. pad            DC.B    0                                ;pad for even addresses
  113. Where        DC.W    0
  114. counter        DC.L    0
  115. WndPtr        DC.L    0                                ;my window pointer
  116. WhichWnd    DC.L    0
  117. clrRect        DC.L    0
  118.             DC.L    0
  119. theStr        DC.L    0                                ;this'll be a string when it grows up
  120.             DC.L    0
  121.             DC.L    0
  122.             DC.L    0
  123.             DC.L    0                                ;that oughta be enough (19 chars + count)
  124.  
  125. ******************************* Handler ******************************
  126.  
  127. myHandler
  128.     
  129.         movem.l        a0/d0,-(a7)
  130.         lea            valid,a0
  131.         clr.b        (a0)                            ;mark counter invalid
  132.         lea            counter,a0
  133.         addq.l        #1,(a0)                            ;bump our counter
  134.         lea            valid,a0
  135.         st.b        (a0)                            ;mark count valid
  136.         MOVE.L        VIA,A0                            ; get VIA base address
  137.         MOVE.B        vT1C(A0),D0                     ;clear interrupt flag
  138.         movem.l        (a7)+,a0/d0
  139.         rts
  140.         
  141. ****************************** Main Code *****************************
  142.  
  143. real    
  144.         btst.b        #7,T1Arbitrate                    ;see if time one is free
  145.         beq            outtahere                        ;hi bit clear means timer in use
  146.         
  147.         move.b        #1,T1Arbitrate                    ;claim it
  148.         
  149.         PEA         thePort                         ;Initialize QuickDraw
  150.         _InitGraf
  151.         _InitFonts                                    ;Initialize Font Manager
  152.         MOVE.L        #$0000FFFF,D0                    ;Discard any previous events
  153.         _FlushEvents                                ;FlushEvents(EventEvent, 0);
  154.         _InitWindows                                ;Initialize Window Manager
  155.  
  156.         clr.l        -(a7)                            ;room for pointer (result)
  157.         move.w        #256,-(a7)                        ;push resource ID
  158.         clr.l        -(a7)                            ;wStorage is nil, let WMgr handle storage
  159.         move.l        #-1,-(a7)                        ;make it the top window
  160.         _GetNewWindow
  161.         lea            WndPtr,a0
  162.         move.l        (a7)+,(a0)                        ;get our pointer
  163.         tst.l        (a0)                            ;make sure it's not nil
  164.         beq            outtahere                        ;nil pointer, so give up
  165.         
  166.         move.l        WndPtr,-(a7)
  167.         _SetPort
  168.         
  169.         lea            oldVector,a0
  170.         move.l        LVL1DT+$18,(a0)                    ;keep the old vector around
  171.         lea            myHandler,a0
  172.         move.l        a0,LVL1DT+$18
  173.         
  174.         lea            clrRect,a0
  175.         move.l        #$00200000,(a0)
  176.         move.l        #$004000FF,4(a0)
  177.         
  178.         MOVE.L        VIA,A0                            ; get VIA base address
  179.  
  180.         OR.B        #$40,VACR(A0)                    ; turn on continous interrupts
  181.         MOVE.W        #100,D0                            ; ticks per half-cycle (<C541 18dec86 mgl> corrected value)
  182.         MOVE.B        D0,vT1C(A0)                     ; Load low byte into counter/latch.
  183.         ROR.W        #8,D0                            ; Get high byte.
  184.         MOVE.B        D0,vT1CH(A0)                    ; Load high byte and off we go!
  185.         or.b        #$C0,vIER(a0)                    ; enable timer 1 interrupts
  186.         
  187. theLoop
  188.         move.w        #5,-(a7)                        ;horizontal point
  189.         move.w        #25,-(a7)                        ;vertival point
  190.         _MoveTo
  191.         
  192.         lea            theStr,a0
  193.         lea            valid,a1
  194. validLoop
  195.         tst.b        (a1)
  196.         beq.s        validLoop
  197.         move.l        counter,d0
  198.         move.w        #0,-(a7)
  199.         _Pack7                                        ;do a NumToString
  200.         
  201.         move.l        WndPtr,a0
  202.         pea            PortRect(a0)
  203.         _EraseRect
  204.         pea            theStr
  205.         _DrawString
  206.         
  207.         clr.w        -(a7)
  208.         _button                                        ;see if mouse is down
  209.         tst.b        (a7)+
  210.         beq.s        theLoop
  211.                         
  212. quit    
  213.         move.l        WndPtr,-(a7)
  214.         _CloseWindow    
  215.  
  216.         MOVE.L        VIA,A0                            ; get VIA base address
  217.         move.b        #$40,vIER(a0)                    ; disable timer 1 interrupts
  218.         MOVE.b        #0,D0                            ;kill de timers?
  219.         MOVE.B        D0,vT1C(A0)                     ; Load low byte into counter/latch.
  220.         MOVE.B        D0,vT1CH(A0)                    ; Load high byte and off we go!
  221.         
  222.         move.l        ticks,d0                        ;give any interrupts laying around a chance…
  223. delayLoop                                            ;call me paraniod
  224.         move.l        ticks,d1
  225.         sub.l        d0,d1
  226.         cmp.l        #10,d1
  227.         ble.s        delayLoop
  228.         move.l        oldVector,LVL1DT+$18            ;restore the old timer 1 vector
  229.         move.b        #$FF,T1Arbitrate                ;mark timer free
  230.         
  231. outtahere
  232.     
  233.         _exitToShell
  234.         END